package com.kvest.gpsservicetracker.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.kvest.gpsservicetracker.R;
import com.kvest.gpsservicetracker.datamodel.DBLocationItem;
import com.kvest.gpsservicetracker.datastorage.GPSDatabase;
import com.kvest.gpsservicetracker.service.GPSTrackerService;
import com.kvest.gpsservicetracker.utils.LoggerService;

import java.util.List;

public class TestActivity extends Activity implements LoggerService.LoggerListener {
    private static final String TAG = "KVEST_TAG";
    private TextView logTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        logTextView = (TextView)findViewById(R.id.log_tv);
    }

    @Override
    public void onResume() {
        super.onResume();

        LoggerService.getInstance().addLoggerListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();

        LoggerService.getInstance().removeLoggerListener(this);
    }

    @Override
    public void onLogUpdated(final String value) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                logTextView.append("\t" + value + "\n");
            }
        });
    }

    public void onStartTrackingClick(View view) {
        GPSTrackerService.startWorkers(getApplicationContext());
        GPSTrackerService.startTracking(getApplicationContext());
    }

    public void onStopTrackingClick(View view) {
        GPSTrackerService.stopTracking(getApplicationContext());
        GPSTrackerService.stopWorkers(getApplicationContext());
    }

    public void onClearLogClick(View view) {
        logTextView.setText("");
    }

    public void onShowDBClick(View view) {
        GPSDatabase gpsDatabase = new GPSDatabase(getApplicationContext());
        List<DBLocationItem> items = gpsDatabase.getAll();
        for (DBLocationItem item : items) {
            Log.d(TAG, item.toString());
        }
    }
}
